﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.IO;
using Microsoft.Phone.Controls.Maps.Platform;
using Microsoft.Phone.Controls.Maps;
using ActivityTracker.Library;

using Common.Library;

namespace ActivityTracker
{
    public partial class MapDisplay : PhoneApplicationPage
    {
        #region Global Variables

        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream isostream;
        XDocument xdoc;
        LocationCollection LocCollection = new LocationCollection();

        string ActivityId = "";
        string historyId = "";

        #endregion

        #region page events

        public MapDisplay()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (NavigationContext.QueryString.TryGetValue("hisId", out historyId))
                {
                    LoadMapWithSavedLocations(historyId);
                }
            }
            catch (Exception ex)
            {
                string mess = "Exception in PhoneApplicationPage_Loaded Method In MapDisplay file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        #endregion

        #region Custom Methods
        //imlemented by bvenuvr on 11/02/2011 
        //here iam getting the location points form Locations/Location_Id.xml File(Id is varing for each activity)
        private void LoadMapWithSavedLocations(string HistoryID)
        {
            try
            {
                string filename = "ActivityTracker/Location_" + HistoryID.ToString() + ".xml";
                if (isoStore.FileExists(filename))
                {
                    isostream = new IsolatedStorageFileStream(filename, FileMode.Open, isoStore);
                    xdoc = XDocument.Load(isostream);
                    isostream.Close();

                    var Locs = from t in xdoc.Descendants("Location")
                               //where t.Attribute("Id").Value == HistoryID
                               select t;
                    if (Locs.Elements().Count() > 0)
                    {
                        foreach (var loc in Locs.Elements("point"))
                        {
                            Location location = new Location();
                            location.Latitude = Convert.ToDouble(loc.Attribute("lattitude").Value);
                            location.Longitude = Convert.ToDouble(loc.Attribute("longitude").Value);

                            Pushpin pushpin1 = new Pushpin();
                            pushpin1.Location = location;
                            MyMapPolyLine.Stroke = new SolidColorBrush(Colors.Green);
                            MyMapPolyLine.StrokeThickness = 5;
                            MyMapPolyLine.Opacity = 0.8;
                            LocCollection.Add(pushpin1.Location);
                            MyMapPolyLine.Locations = LocCollection;

                            if (Convert.ToInt32(loc.Attribute("pointno").Value) == 1)
                            {
                                mymap.Children.Add(pushpin1);
                                if (loc.Attribute("pointno") != null)
                                    pushpin1.Content = "Start";
                            }
                            if (Convert.ToInt32(loc.Attribute("pointno").Value) == Locs.Elements().Count())
                            {
                                mymap.Children.Add(pushpin1);
                                if (loc.Attribute("pointno") != null)
                                    pushpin1.Content = "End";
                            }
                            pushpin1.Background = new SolidColorBrush(Colors.Red);
                            pushpin1.MaxHeight = 100;
                            pushpin1.MaxWidth = 100;
                            mymap.Center = location;
                            mymap.ZoomLevel = 14;

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Exception exnew = new Exception("Exception in LoadMapWithSavedLocations Method In MapDisplay file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                exnew.Data.Add("ActivityId", ActivityId);
                exnew.Data.Add("historyId", HistoryID);
                throw exnew;
            }
        }

        #endregion
    }
}